$(document).ready(function() {
//	$("#t_devis img[title]").tooltip();
	$('textarea.tinymce').tinymce({
		script_url : '/public/javascript/tinymce/jscripts/tiny_mce/tiny_mce.js',
		theme : "simple"
	});
	$("#carnetAddress").fixheadertable({
		colratio: [30,'',''],
		zebra : true,
		zebraClass : 'ui-state-active',
		sortable: true,
		sortType : ['string','string'],
		pager: true,
		rowsPerPage: 25
	});
	$('#t_courriers').fixheadertable({
		colratio: [30,100,200,'',50],
		zebra : true,
		zebraClass : 'ui-state-active',
		sortable: true,
		sortType : ['integer','string','date','integer','string'],
		pager: true,
		rowsPerPage: 25,
		
	});
	$("#form_courrier_dial").dialog({
		autoOpen: false,
		modal: true,
		width: '700px',
		resizable: false,
		buttons: {
			"Fermer": function(){
				$(this).dialog('close');
			},
			"Enregistrer": function(){
				saveCourrier();
			}
		}
	});
	$("#dial_courrier_ok").dialog({
		autoOpen: false,
		modal: true,
		resizable: false,
		buttons: {
			"OK": function(){
				window.location = "/impressions?courriers";
			}
		}
	});
	$(".datepicker").datepicker({
		dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
		monthNames: ['Janvier','F"vrier','Mars','Avril','Mai','Juin','Juillet','Aout','Septembre','Octobre','Novembre','Decembre'],
		dayNamesMin: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
		dateFormat: 'dd-mm-yy'
	});
	$("#selectPeriod").dialog({
		autoOpen: false,
		modal: true,
		resizable: false,
		width: '500px',
		buttons: {
			"Fermer": function(){
				$(this).dialog('close');
			},
			"Télécharger": function(){
				$("#downloadForm").submit();
				$(this).dialog('close');
			}
			
		}
	});
});
function select(name){
	$("#downloadForm").attr("action","/download/"+name);
	$("#selectPeriod").dialog('open');
}
function openDialCourrier(){
	$("#form_courrier_dial").dialog('open');
}
function saveCourrier(){
	var postVars = $("#form_courrier").serializeArray();
	$.ajax({
        type: "POST",
        url: '/impressions/courrier/add',
        dataType: "json",
        data: postVars,
        success: function(data){
        	if(data.response_code == "ok"){
        		alert("Courrier enregistré");
        		window.location = "/impressions?courriers";
        	}else{
        		alert("Une erreur est survenue");
        	}
        }
	});
}
function removeCourrier(id){
	if(confirm("Supprimer le courrier?")){
		$.ajax({
	        type: "POST",
	        url: '/impressions/courrier/remove/'+id,
	        dataType: "json",
	        success: function(data){
	        	if(data.response_code == "ok"){
	        		$("#dial_courrier_ok").html("Courrier supprimé");
	        		$("#dial_courrier_ok").dialog("open");
	        	}else{
	        		alert("Une erreur est survenue");
	        	}
	        }
		});
	}
}